package GUI;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.LineBorder;

import MessagePacks.GetAllPack;
import MessagePacks.MessagePack;
import MessagePacks.OneProcessPack;
import MessagePacks.getCommitteePack;

import java.awt.Color;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;

import entities.CProcess;
import javax.swing.JTextField;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class MainScreenEngineer extends MainScreenStudent {

	private static final long serialVersionUID = 1L;
	private MainGUI maingui;
	private JButton btnTreatProcess;
	private JPanel panel;
	private ProcessSelectionScreen prccreen;
	private JButton btnReadReport;
	private getCommitteePack getCom;
	private int committeeFlag=0;
	private String com="Chairman";
	private EstimationReportRead readRep;
	private JPanel panel_1;
	private JComboBox<String> comboBox;
	private JLabel lblProcessId;
	private JButton btnChooseProc;
	private GetAllPack estRp;
	private GetAllPack allProcess;
	private String member= "Not member";
	private OneProcessPack pck,pck2;
	private JButton btnTraceAllProcesses;
	private JButton btnAccept;
	private JComboBox<String> comboBox_1;
	private JPanel panel_2;
	private JPanel panel_3;
	private JButton btnOk;
	private JLabel lblNewLabel;
	private JLabel lblInitiatorId;
	private JLabel lblStatus;
	private JLabel lblCurrentPhase;
	private JLabel lblStartDate;
	private JLabel lblEndDate;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	
	public MainScreenEngineer(MainGUI mainGui) {
		
		super(mainGui);
		this.maingui=mainGui;
		
		getCommitteeMembers();
		setMember();
		getReports();
		
		panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel.setBounds(0, 153, 176, 112);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		panel_1 = new JPanel();
		panel_1.setBounds(211, 83, 226, 83);
		getContentPane().add(panel_1);
		panel_1.setLayout(null);
		panel_1.setVisible(false);
		

		
		
		lblProcessId = new JLabel("Process ID");
		lblProcessId.setHorizontalAlignment(SwingConstants.CENTER);
		lblProcessId.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblProcessId.setBounds(10, 11, 113, 19);
		panel_1.add(lblProcessId);
		
		panel_2 = new JPanel();
		panel_2.setBounds(480, 83, 226, 83);
		getContentPane().add(panel_2);
		panel_2.setLayout(null);
		panel_2.setVisible(false);
		
		JLabel lblProcessId_1 = new JLabel("Process ID");
		lblProcessId_1.setHorizontalAlignment(SwingConstants.CENTER);
		lblProcessId_1.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblProcessId_1.setBounds(0, 11, 114, 22);
		panel_2.add(lblProcessId_1);
		
		panel_3 = new JPanel();
		panel_3.setBounds(314, 177, 300, 469);
		getContentPane().add(panel_3);
		panel_3.setLayout(null);
		panel_3.setVisible(false);
		
		setBtnOk();
		

		getAllProcforsup();
		setBtnAccept();
		setComboBox_1();
		
		
		setBtnChooseProc();
		setProcComboBox();
		
		
		setBtnTraceAllProcesses();
		setBtnTreatProcess();
		setbtnReadReport();
		
	}

	private void setPanel3() {
		{
			lblNewLabel = new JLabel("Process ID :");
			lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblNewLabel.setBounds(10, 11, 105, 23);
			panel_3.add(lblNewLabel);
		}
		{
			lblInitiatorId = new JLabel("Initiator ID : ");
			lblInitiatorId.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblInitiatorId.setBounds(10, 64, 105, 27);
			panel_3.add(lblInitiatorId);
		}
		{
			lblStatus = new JLabel("Status :");
			lblStatus.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblStatus.setBounds(10, 118, 89, 23);
			panel_3.add(lblStatus);
		}
		{
			lblCurrentPhase = new JLabel("Current phase :");
			lblCurrentPhase.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblCurrentPhase.setBounds(10, 174, 128, 23);
			panel_3.add(lblCurrentPhase);
		}
		{
			lblStartDate = new JLabel("Start date :");
			lblStartDate.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblStartDate.setBounds(10, 232, 105, 23);
			panel_3.add(lblStartDate);
		}
		{
			lblEndDate = new JLabel("End date : ");
			lblEndDate.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblEndDate.setBounds(10, 291, 105, 23);
			panel_3.add(lblEndDate);
		}
		{
			textField = new JTextField();
			textField.setBounds(157, 14, 110, 20);
			panel_3.add(textField);
			textField.setText(String.valueOf(maingui.process.getprocessID()));
			textField.setColumns(10);
			textField.setEditable(false);
		}
		{
			textField_1 = new JTextField();
			textField_1.setColumns(10);
			textField_1.setBounds(157, 69, 110, 20);
			textField_1.setText(String.valueOf(maingui.process.getinitiatorID()));
			panel_3.add(textField_1);
			textField_1.setEditable(false);
		}
		{
			textField_2 = new JTextField();
			textField_2.setColumns(10);
			textField_2.setBounds(157, 121, 110, 20);
			textField_2.setText(maingui.process.getStatus());
			panel_3.add(textField_2);
			textField_2.setEditable(false);
		}
		{
			textField_3 = new JTextField();
			textField_3.setColumns(10);
			textField_3.setBounds(157, 177, 110, 20);
			textField_3.setText(String.valueOf(maingui.process.getCurrentPhase()));
			panel_3.add(textField_3);
			textField_3.setEditable(false);
		}
		{
			textField_4 = new JTextField();
			textField_4.setColumns(10);
			textField_4.setBounds(157, 235, 110, 20);
			textField_4.setText(maingui.process.getStartDate());
			panel_3.add(textField_4);
			textField_4.setEditable(false);
		}
		{
			textField_5 = new JTextField();
			textField_5.setColumns(10);
			textField_5.setBounds(157, 294, 110, 20);
			textField_5.setText(maingui.process.getEndDate());
			panel_3.add(textField_5);
			textField_5.setEditable(false);
		}
		
	}

	public JButton getBtnTreatProcess() {
		return btnTreatProcess;
	}



	private void setBtnTreatProcess() {
		btnTreatProcess = new JButton("Treat Process");
		btnTreatProcess.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(serverGUI.mainframe,"button treat process pressed");
				prccreen = new ProcessSelectionScreen(maingui);
				prccreen.setVisible(true);
			    close();	
			}
		});
		btnTreatProcess.setBounds(10, 11, 156, 23);
		panel.add(btnTreatProcess);
		

	}

	
	private void setbtnReadReport(){
		btnReadReport = new JButton("Read Report");
		if(committeeFlag==0)
			btnReadReport.setEnabled(false);
		else 
			btnReadReport.setEnabled(true);
		btnReadReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(serverGUI.mainframe,"button Read Report pressed");
				panel_1.setVisible(true);
			}
		});
		btnReadReport.setBounds(10, 45, 156, 23);
		panel.add(btnReadReport);
		

	}
	
	private void getCommitteeMembers(){
		getCom = new getCommitteePack();
		maingui.client.handleMessageFromClientUI(getCom);
		getCom= (getCommitteePack) maingui.client.getMessage();
	}
	
	private void getReports(){
		if(member.equals("Member1")){
			estRp = new GetAllPack(MessagePack.getEsReportsMem1);
			 maingui.client.handleMessageFromClientUI(estRp);
			 estRp= (GetAllPack) maingui.client.getMessage();}
		else if(member.equals("Member2")){
			estRp = new GetAllPack(MessagePack.getEsReportsMem2);
			 maingui.client.handleMessageFromClientUI(estRp);
			 estRp= (GetAllPack) maingui.client.getMessage();}
		 
	}

	public JButton getBtnChooseProc() {
		return btnChooseProc;
	}

	public void setBtnChooseProc() {
		btnChooseProc = new JButton("Accept");
		btnChooseProc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int prcID3= Integer.parseInt((String) comboBox.getSelectedItem());
				setProcPck(prcID3);
				maingui.process=new CProcess(pck.getProcessID(),pck.getInitiatorID() ,pck.getSupervisorID() ,pck.getEstimatorID() ,pck.getChairmanID() ,pck.getExecLeaderID() ,pck.getTesterID() ,pck.getStatus(),pck.getCurrentPhase(),pck.getResponsableOK() );
				JOptionPane.showMessageDialog(serverGUI.mainframe,"Process selected");
				readRep = new EstimationReportRead(maingui);
				readRep.setVisible(true);
				close();
			}
		});
		btnChooseProc.setBounds(133, 40, 76, 23);
		
		panel_1.add(btnChooseProc);
	}

	public JComboBox<String> getProcComboBox() {
		return comboBox;
	}

	public void setProcComboBox() {
		comboBox = new JComboBox();
		comboBox.setBounds(20, 41, 83, 20);
		comboBox.addItem("");
		if(member.equals("Member2")||(member.equals("Member1"))){
			Iterator<String> iter = estRp.nArray.iterator();
			while (iter.hasNext()){
				String procId = iter.next();
				comboBox.addItem(procId);
			}
		}
		panel_1.add(comboBox);
	}

	public String getMember() {
		return member;
	}

	public void setMember() {
		Iterator<Entry<String, String>> iter = getCom.getMyHashMap().entrySet().iterator();
		while (iter.hasNext()){
			Map.Entry mEntry = (Map.Entry) iter.next();
			String value =  (String) mEntry.getValue();
			if(Integer.parseInt((String) mEntry.getKey())==MainGUI.usr.GetUser() && !com.equals(value)){
				member=value;
				committeeFlag=1;
			}
		}
	
	}
	
	/**
	 * 
	 * @param prcID2 - Process's ID
	 */
	public void setProcPck(int prcID2) {
		pck= new OneProcessPack(prcID2);
		maingui.client.handleMessageFromClientUI(pck);
		pck= (OneProcessPack) maingui.client.getMessage();
	}

	public JButton getBtnTraceAllProcesses() {
		return btnTraceAllProcesses;
	}

	public void setBtnTraceAllProcesses() {
		btnTraceAllProcesses = new JButton("Trace all processes");
		btnTraceAllProcesses.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				panel_2.setVisible(true);
				panel_1.setVisible(false);
				
			}
		});
		btnTraceAllProcesses.setBounds(10, 78, 156, 23);
		panel.add(btnTraceAllProcesses);
	}

	public JButton getBtnAccept() {
		return btnAccept;
	}

	public void setBtnAccept() {
		btnAccept = new JButton("Accept");
		btnAccept.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int procID= Integer.parseInt((String) comboBox_1.getSelectedItem());
				setProcPck2(procID);
				maingui.process=new CProcess(pck2.getProcessID(),pck2.getInitiatorID() ,pck2.getSupervisorID() ,pck2.getEstimatorID() ,pck2.getChairmanID() ,pck2.getExecLeaderID() ,pck2.getTesterID() ,pck2.getStatus(),pck2.getCurrentPhase(),pck2.getResponsableOK() );
				maingui.process.setStartDate(pck2.getStartDate());
				maingui.process.setEndDate(pck2.getEndDate());
				panel_2.setVisible(false);
				setPanel3();
				panel_3.setVisible(true);
				
			}
			
		});
		btnAccept.setBounds(125, 43, 89, 23);
		panel_2.add(btnAccept);
	}
	
	public void setProcPck2(int prcID2) {
		pck2= new OneProcessPack(prcID2);
		maingui.client.handleMessageFromClientUI(pck2);
		pck2= (OneProcessPack) maingui.client.getMessage();
	}

	public JComboBox<String> getComboBox_1() {
		return comboBox_1;
	}

	public void setComboBox_1() {
		comboBox_1 = new JComboBox<String>();
		comboBox_1.setBounds(10, 44, 89, 20);
		comboBox_1.addItem("");
		Iterator<String> iter2 = allProcess.nArray.iterator();
		while (iter2.hasNext()){
			String procId2 = iter2.next();
			comboBox_1.addItem(procId2);}
		
		panel_2.add(comboBox_1);
	}

	private void getAllProcforsup() {
		allProcess = new GetAllPack(MessagePack.getAllProcOfSup);
		allProcess.setSupID(maingui.usr.GetUser());
		maingui.client.handleMessageFromClientUI(allProcess);
		allProcess= (GetAllPack) maingui.client.getMessage();
		
	}

	public JButton getBtnOk() {
		return btnOk;
	}

	public void setBtnOk() {
		btnOk = new JButton("Ok");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panel_3.setVisible(false);
			}
		});
		btnOk.setBounds(98, 391, 89, 23);
		panel_3.add(btnOk);
		
	}
}
